<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/22
 * Time: 下午4:07
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
use Lib\MyTree;
use Lib\Tree;

class RuleController extends AdminBaseController
{
    /**
     * 权限表
     */
    public function index()
    {
        $curr = I('get.p/d',0);
        $totalNum = 10;
        $Rule = M('Rule');
        $rule_list = $Rule->page($curr.','.$totalNum)->select();
        $this->assign('rule_list',$rule_list);
        $count = $Rule->count();
        $Page  = new \Think\Page($count,$totalNum);
        $show  = $Page->show();
        $this->assign('page',$show);
        $this->assign('title','FD商城权限表');
        $this->display();

    }

    /**
     * 添加权限
     */
    public function add()
    {
        if(IS_GET){
            $rule_data = M('rule')->field('id,title,pid')->where(['status'=>1])->order('pid asc')->select();
            $my_tree = new MyTree();
            $rule_info = $my_tree->my_tree($rule_data);
            $this->assign('rule_info',$rule_info);
            $this->assign('title','FD商城权限添加');
            $this->display();
            exit;
        }
        if(IS_AJAX){
            $rule_name = I('post.rule_name','');
            if(empty($rule_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'权限唯一标识不能为空！']);
            }
            //获取权限中文名称
            $title = I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'权限中文名称不能为空！']);
            }
            //获取父级id
            $pid = I('post.pid',0);
            if(M('rule')->where(array('name'=>$rule_name))->find()){
                $this->ajaxReturn(['state'=>1,'msg'=>'此权限已经存在！']);
            }
            if(M('rule')->add(['name'=>$rule_name,'title'=>$title,'pid'=>$pid])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加权限成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加权限失败！']);
        }
    }

    /**
     * 修改权限
     */
    public function edit()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $rule_info = M('rule')->field('id,name,title,pid,condition')->where(['id'=>$id])->find();
            $rule_data = M('rule')->field('id,title,pid')->where(['status'=>1])->order('pid asc')->select();
            $my_tree = new MyTree();
            $all_rule = $my_tree->my_tree($rule_data);
            $this->assign('all_rule',$all_rule);
            $this->assign('rule_info',$rule_info);
            $this->assign('title','FD商城权限修改');
            $this->display();
        }
        if(IS_POST){
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //获取权限唯一标识
            $rule_name = I('post.rule_name','');
            if(empty($rule_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'权限唯一标识不能为空！']);
            }
            //获取权限中文名称
            $title = I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'权限中文名称不能为空！']);
            }
            $status = I('post.status',1);
            $condition = I('post.condition','');
            $date = [
                'name'      =>  $rule_name,
                'title'     =>  $title,
                'status'    =>  $status,
                'condition' =>  $condition
            ];
            if(M('rule')->where(['id'=>$id])->setField($date) === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
        }
    }

    /**
     * 删除权限
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            if(M('rule')->where(['id'=>$id])->getField('id') == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的权限！']);
            }
            if(M('rule')->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }

    /**
     * 修改权限状态
     */
    public function changeState(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $rule_status = M('rule')->where(['id' => $id])->getField('status');
            if($rule_status === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改状态失败！']);
            }
            if($rule_status){
                M('rule')->where(['id' => $id])->setField('status',0);
                $this->ajaxReturn(['state'=>0,'msg'=>'已禁用！','data' => 1]);
            }else{
                M('rule')->where(['id' => $id])->setField('status',1);
                $this->ajaxReturn(['state'=>0,'msg'=>'已启用！','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }

    /**
     * 为角色分配权限
     */
    public function group()
    {
        if(IS_POST){
            $data=I('post.');
            $map=array(
                'id'=>$data['id']
            );
            $data['rules']=implode(',', $data['rule_ids']);
            $result=D('group')->editData($map,$data);
            if ($result) {
                $this->success('操作成功',U('group/index'));
            }else{
                $this->error('操作失败');
            }
        }else{
            $id=I('get.id');
            // 获取用户组数据
            $group_data = M('group')->where(['id'=>$id])->find();
            $group_data['rules'] = explode(',', $group_data['rules']);
            // 获取规则数据
            $rule_data = D('rule')->getTreeData('level','id','title');
            $assign=array(
                'group_data'=>$group_data,
                'rule_data'=>$rule_data
            );
            $this->assign($assign);
            $this->display();
        }
    }


}